$(function () {
    vm.getTypeTree();
});

var typeTree;
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "superType",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    },
    callback: {
        onClick: function () {
            var node = typeTree.getSelectedNodes();
            vm.q.mouldTypeId = node[0].id;
            vm.query();
        }

    }
};
var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            month: [],
            mouldTypeId: '',//模具类型
            topNum: 10
        },
        option: {
            title: {
                text: '模具故障处理时长统计',
                left: 'center'
            },
            tooltip: {
                trigger: 'axis'
            },
            toolbox: {
                show: true,
                feature: {
                    magicType: {show: true, type: ['line', 'bar']},
                    dataView: {show: true, readOnly: false},
                    saveAsImage: {show: true}
                },
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function (params) {
                    var tar = params[1];
                    return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value;
                }
            },
            legend: {
                data: ['故障处理时间', '故障时间'],
                top: '30px'
            },
            xAxis: [
                {
                    type: 'category',
                    data: []
                }
            ],
            yAxis: [
                {type: 'value'},
                {type: 'value'}
            ],
            series: [
                {
                    name: '故障时间',
                    type: 'bar',
                    data: []
                },
                {
                    name: '故障处理时间',
                    type: 'bar',
                    data: []
                }
            ]
        }
    },
    methods: {
        query: function () {
            this.reload();
        },
        reload: function () {
            var param = {};
            if (this.q.month && this.q.month.length == 2) {
                if(this.q.month[0]){
                    param.startDate = dateToString(this.q.month[0]) + ' 0:00:00';
                }
                if(this.q.month[1]){
                    param.endDate = dateToString(this.q.month[1]) + ' 23:59:59';
                }
            }
            if(this.q.mouldTypeId){
                param.mouldTypeId = this.q.mouldTypeId;
            }
            if(this.q.topNum){
                param.topNum = this.q.topNum;
            }
            myChart.showLoading();
            Ajax.request({
                url: '../mould/stat/faultDurationAnalyse',
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    myChart.hideLoading();
                    // 故障时长统计
                    var faultDurationStat = res.result;
                    var option = {
                        xAxis: {
                            data: faultDurationStat.xaxisData
                        },
                        series: [
                            {data: faultDurationStat.barSeriesData[0].data},
                            {data: faultDurationStat.barSeriesData[1].data}
                        ]
                    };
                    myChart.setOption(option);
                }
            });
        },
        reloadSearch: function () {
            this.q = {
                month: [],
                mouldTypeId : '',
                topNum: 10
            };
            // 取消选中
            typeTree.cancelSelectedNode();
            this.reload();
        },
        getTypeTree: function () {
            //加载模具类型树
            Ajax.request({
                url: "../moduletype/select",
                async: true,
                successCallback: function (r) {
                    typeTree = $.fn.zTree.init($("#typeTree"), setting, r.typeList);
                    typeTree.expandAll(true);
                }
            });
        }
    },
    mounted: function () {
        // 初始化处理时长分析报表
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option, true);
        // 初始化加载数据
        this.query();
    }
});
